/*
 *	작성날짜 : 10월 16일
 *	작성자 : 이민선 	
 */
package jangdolbaeng2.customer.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import jangdolbaeng2.customer.dao.CartDAO;
import jangdolbaeng2.customer.dao.CustomerDAO;
import jangdolbaeng2.tools.ClobToString;
import jangdolbaeng2.vo.CartVO;
import jangdolbaeng2.vo.CustomerVO;
import jangdolbaeng2.vo.DeliveryVO;
import jangdolbaeng2.vo.Order_detailVO;
import jangdolbaeng2.vo.ProductVO;
import jangdolbaeng2.vo.Product_orderVO;
import jangdolbaeng2.vo.RealCartVO;
import oracle.sql.CLOB;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class CartAction extends ActionSupport implements SessionAware{
	private CustomerVO customer;
	private CartVO cart;
	private ProductVO product;
	private Map<String, Object> session;
	private List<HashMap<String, Object>> cartList;
	private int product_seq;
	private String customer_id;
	private Product_orderVO product_order;
	private DeliveryVO delivery;
	private int order_seq;
	private Order_detailVO order_detail;
	private List<Integer> product_seqList;
	private List<Integer> quantityList;

	private List<RealCartVO>  realCart;
	private RealCartVO realCartVO;
	private int realCartSize;
	
	/* --------------변수 작성자 : 문연지-------------- */
	private int quantity; 
	private CartVO newcart;

	
	//장바구니 목록
	public String selectCart() throws Exception {
		//System.out.println("들어왔따2");
		customer_id = (String) session.get("customer_id");
		
		realCart = new CartDAO().selectCart(customer_id);
		
		realCartSize = realCart.size();
		//System.out.println(realCartSize + "개의 상품리스트");
		//System.out.println("cartList Action : " + realCart);

		return SUCCESS;
	}
	
	//주문내역 & 주문자 정보
	public String selectOrder() throws Exception{
		customer_id = (String) session.get("customer_id");

		customer = new CustomerDAO().getCustomer(customer_id);
		System.out.println("Action> selectOrder(): customer : " + customer);
		product_seqList = (List<Integer>) session.get("product_seqList");
		quantityList = (List<Integer>) session.get("quantityList");
		
		realCart = new ArrayList<RealCartVO>(); 
		
		for (int i = 0; i < product_seqList.size(); i++) {
			realCartVO = new RealCartVO();
			
			realCartVO.setCustomer_id(customer_id);
			
			product = new CartDAO().getProduct(product_seqList.get(i));
			
			realCartVO.setProduct_seq(product_seqList.get(i));
			realCartVO.setQuantity(quantityList.get(i));
			realCartVO.setProduct_name(product.getProduct_name());
			realCartVO.setProduct_image1(product.getProduct_image1());
			realCartVO.setProduct_image2(product.getProduct_image2());
			realCartVO.setProduct_price(product.getProduct_price());
			System.out.println("Action> selectOrder(): realCartVO : " + realCartVO);
			
			realCart.add(realCartVO);
		}

		System.out.println("Action> selectOrder(): realCart : " + realCart);	
		session.put("realCart", realCart);
		
		return SUCCESS;
	}
	
	//product_order 저장
	public String insertPro_Order() throws Exception{
		System.out.println("Action> insertPro_Order(): product_order1 : "+ product_order);
		
		order_seq = new CartDAO().getSeq();
		session.put("order_seq", order_seq);
		product_order.setOrder_seq(order_seq);
		session.put("product_order", product_order);
		
		System.out.println("Action> insertPro_Order(): insert order_seq : " + order_seq);
		System.out.println("Action> insertPro_Order(): product_order : " + product_order);
		
		new CartDAO().insertPro_Order(product_order);
		
		realCart = (List<RealCartVO>) session.get("realCart");
		product_order =  (Product_orderVO) session.get("product_order");
		
		for(RealCartVO vo: realCart) {
			vo.setOrder_seq(order_seq);			
		}//for each
		
		new CartDAO().insertOrderDetail(realCart);
		
	    return SUCCESS;

	 }
	      
	//배송지정보 저장
	public String insertDeli() throws Exception{
		new CartDAO().insertDeli(delivery);
	    order_seq = delivery.getOrder_seq();
	    System.out.println("Action> insertDeli(): order_seq : "+ order_seq);
	    return SUCCESS;
	 }
	 
	//주문완료 페이지 이동
	public String orderFinish() throws Exception {
		customer_id = (String) session.get("customer_id");
		customer = new CustomerDAO().getCustomer(customer_id);
		delivery = new CartDAO().selectDeli(order_seq);
		realCart = (List<RealCartVO>) session.get("realCart");
		product_order =  (Product_orderVO) session.get("product_order");
		System.out.println("Action> orderFinish() : delivery : " + delivery);
		System.out.println("Action> orderFinish() : product_order : " + product_order);
		System.out.println("Action> orderFinish() : customer : " + customer);
		new CartDAO().allDeleteCart(customer_id);
		System.out.println("Action> orderFinish() : customer_id : " + customer_id);
		return SUCCESS;
	}
	 
	//주문내역(order_detail) 저장
	public String insertOrderDetail() {
		try{
			new CartDAO().insertOrderDetail(realCart);
		}catch(Exception e){
			e.printStackTrace();
		}
			System.out.println("Action> insertOrderDetail() : order_detail : " + order_detail);
			return SUCCESS;
		}
	   
	//장바구니에서 수량 증가 저장
	public String updateQuantity() throws Exception{
		//System.out.println("들어왔다");
		customer_id = (String )session.get("customer_id");
			
		newcart = new CartVO();		 
		newcart.setCustomer_id(customer_id);
		newcart.setProduct_seq(product_seq);
		newcart.setQuantity(quantity);

		//System.out.println(quantity);
		new CartDAO().updateCart(newcart);
		//System.out.println("Action newcart : " + newcart);
		return this.selectCart();
	 }
	   
	//장바구니에서 선택 상품 삭제
	public String deleteCart() throws Exception{
		//System.out.println("들어왔다 deleteCart");
		customer_id = (String)session.get("customer_id");
		//System.out.println("Action deleteCart customer_id : " + customer_id);
		//System.out.println("Action product_seqList : " + product_seqList);
		for (int i = 0; i < product_seqList.size(); i++) {
			cart = new CartVO();
			cart.setCustomer_id(customer_id);
			cart.setProduct_seq(product_seqList.get(i));
			
			new CartDAO().deleteCart(cart);
			//System.out.println("Action cart : " + cart);
		}
		return SUCCESS;
	}
	
	//선택상품 주문 
	public String choiceOrder() throws Exception {	
//		customer_id = (String) session.get("customer_id");
		
		System.out.println("welcome");
		System.out.println(product_seqList + " 상품 번호 리스트");
		System.out.println(quantityList + " 수량 리스트");
		System.out.println(product_seqList.size() + "size");
		
		session.put("product_seqList", product_seqList);
		session.put("quantityList", quantityList);
		
		
		return SUCCESS;
	}
	   
	/*-------------------------------메소드 작성자: 문연지------------------------------*/
	//장바구니 테이블에 저장
	public String insertCart(){
		customer_id = (String)session.get("customer_id");
		
		newcart = new CartVO();
		
		newcart.setCustomer_id(customer_id);
		newcart.setProduct_seq(product_seq);
		newcart.setQuantity(quantity);
		
		cart = new CartDAO().selectACart(newcart);
		
		if(cart == null){
			new CartDAO().insertCart(newcart);
			
			System.out.println("입력" + cart);
		}else{
			quantity = cart.getQuantity() + quantity;
			System.out.println(quantity);
			newcart.setQuantity(quantity);
			
			new CartDAO().updateCart(newcart);
			
			System.out.println("추가"+newcart);
		}
		return SUCCESS;
	}
	
	
	//getter & setter
	public CartVO getCart() {
		return cart;
	}

	public void setCart(CartVO cart) {
		this.cart = cart;
	}

	public ProductVO getProduct() {
		return product;
	}

	public void setProduct(ProductVO product) {
		this.product = product;
	}

	public Map<String, Object> getSession() {
		return session;
	}

	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	public List<HashMap<String, Object>> getCartList() {
		return cartList;
	}

	public void setCartList(List<HashMap<String, Object>> cartList) {
		this.cartList = cartList;
	}

	public int getProduct_seq() {
		return product_seq;
	}

	public void setProduct_seq(int product_seq) {
		this.product_seq = product_seq;
	}

	public String getCustomer_id() {
		return customer_id;
	}

	public void setCustomer_id(String customer_id) {
		this.customer_id = customer_id;
	}

	public CustomerVO getCustomer() {
		return customer;
	}

	public void setCustomer(CustomerVO customer) {
		this.customer = customer;
	}

	public Product_orderVO getProduct_order() {
		return product_order;
	}

	public void setProduct_order(Product_orderVO product_order) {
		this.product_order = product_order;
	}

	public DeliveryVO getDelivery() {
		return delivery;
	}

	public void setDelivery(DeliveryVO delivery) {
		this.delivery = delivery;
	}

	public int getOrder_seq() {
		return order_seq;
	}

	public void setOrder_seq(int order_seq) {
		this.order_seq = order_seq;
	}

	public Order_detailVO getOrder_detail() {
		return order_detail;
	}

	public void setOrder_detail(Order_detailVO order_detail) {
		this.order_detail = order_detail;
	}

	public List<Integer> getProduct_seqList() {
		return product_seqList;
	}

	public void setProduct_seqList(List<Integer> product_seqList) {
		this.product_seqList = product_seqList;
	}

	public List<Integer> getQuantityList() {
		return quantityList;
	}

	public void setQuantityList(List<Integer> quantityList) {
		this.quantityList = quantityList;
	}

	public RealCartVO getRealCartVO() {
		return realCartVO;
	}

	public void setRealCartVO(RealCartVO realCartVO) {
		this.realCartVO = realCartVO;
	}

	public int getRealCartSize() {
		return realCartSize;
	}

	public void setRealCartSize(int realCartSize) {
		this.realCartSize = realCartSize;
	}

	/* -----------------------------------작성자 : 문연지---------------------------------------------- */
	public int getQuantity() {
		return quantity;
	}
	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}
	public CartVO getNewcart() {
		return newcart;
	}
	public void setNewcart(CartVO newcart) {
		this.newcart = newcart;
	}

	public List<RealCartVO> getRealCart() {
		return realCart;
	}

	public void setRealCart(List<RealCartVO> realCart) {
		this.realCart = realCart;
	}
	
}
